package KK.Animation
{
	import KK.Time.FrameWheel;
	
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;

	public class SpriteSheet extends SheetBase
	{
		static private const PLAY:int=0x2;
		static private const LOOP_MAX:int=0x0;
		static private const NORMAL:int=0x1; 
		
		private var _sheetWidth:uint;
		private var _sheetHeight:uint;
		private var _spriteWidth:uint;
		private var _spriteHeight:uint;
		
		private var AreaTable:Vector.<Rectangle>;//区域表
		
		private var animationMap:Dictionary;
		private var mapSite:String;//动画名称
		
		
		private var state:int;//状态表 
		private var frame:int;//位置
		public function SpriteSheet(sheetWidth:uint,sheetHeight:uint,spriteWidth:uint,spriteHeight:uint,frameRate:uint=24, loop:Boolean=true)
		{
			//TODO: implement function
			
			animationMap=new Dictionary();
			this._sheetWidth=sheetWidth;
			this._sheetHeight=sheetHeight;
			this._spriteWidth=spriteWidth;
			this._spriteHeight=spriteHeight;
			
			
			super(frameRate, loop);
			
			
			generateSheet(); 
		}
		
		
		
		/**
		 * 播放下一个对象  
		 * 
		 */
		public function PlayNext():void{
			const len:int=animationMap[mapSite].length;
			frame++;
			if(frame>=len){
				frame=0;
				!loop&&finish();
			}

		}
		
		/**
		 * 获取区域 
		 * @return 
		 * 
		 */
		public function get Area():Rectangle{
			return AreaTable[animationMap[mapSite][frame]];
		}
		
		
		/**
		 * 添加动画 
		 * @param key
		 * @param list
		 * 
		 */
		public function addAnimation(key:String,list:Array):void{
			animationMap[key]=list;
		}
		
		/**
		 * 移除动画  
		 * @param key
		 * 
		 */
		public function removeAnimation(key:String):void{
			delete animationMap[key];
		}
		/**
		 * 播放动画名 
		 * @param key
		 * 
		 */
		public function playAnimation(key:String):void{
			mapSite=key;
			
			frame=0;
			play();
		}
		
		
		
		/**
		 * 剪裁区域 
		 * 
		 */
		
		private function generateSheet():void {
			var numSheetsPerRow:int = Math.round(_sheetWidth / _spriteWidth);
			var numRows:int = Math.round(_sheetHeight / _spriteHeight);
			var numSheets:int = numSheetsPerRow * numRows;
			var rowIdx:uint;
			var colIdx:uint;
			
			AreaTable=new Vector.<Rectangle>(numSheets,true);
			
			
			for(var i:int = 0; i < numSheets; i++) {
				rowIdx = i % numSheetsPerRow;
				colIdx = Math.floor(i / numSheetsPerRow);
				
				AreaTable[i]=(new Rectangle((_spriteWidth * rowIdx),
					(_spriteHeight * colIdx),
					_spriteWidth,
					_spriteHeight));
			}


			frame=0;
		}
		
		
		
		/**
		 * 游戏循环  
		 * 
		 */
		override protected function GameLoop():void{
			if(checkPlay()&&checkContinue()){
				PlayNext();
			}
		}
		
		
		/**
		 * 检查是否播放  
		 * @return 
		 * 
		 */
		private function checkPlay():Boolean{
			var result:Boolean=(state&0x2)==PLAY;
			
			
			return result;
		}
		/**
		 * 是否继续显示 
		 * @return 
		 * 
		 */
		private function checkContinue():Boolean{
			return (state&0x1)==LOOP_MAX;
		}
		/**
		 * 更改完成状态  
		 * 
		 */
		private function finish():void{
			state=(state&0xFFFFFFFE)|NORMAL
		}
		
		/**
		 * 播放  
		 * @return 
		 * 
		 */
		private function play():void{
			state=(state&0xFFFFFFFD)|PLAY;
		}
	}
}